package com.ming.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.CodeMsg;
import com.ming.common.Constant;
import com.ming.common.Result;
import com.ming.common.realm.ActiveUser;
import com.ming.domain.Book;
import com.ming.domain.BookRent;
import com.ming.mapper.BookMapper;
import com.ming.mapper.BookRentMapper;
import com.ming.query.BookReturnQuery;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mapper.BookReturnMapper;
import com.ming.domain.BookReturn;
import com.ming.service.BookReturnService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-05-05 22:15
 */
@Service
public class BookReturnServiceImpl extends ServiceImpl<BookReturnMapper, BookReturn> implements BookReturnService{

    @Autowired
    private BookReturnMapper bookReturnMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookRentMapper bookRentMapper;

    /**
     * 分页查询还书记录
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BookReturnQuery query) {

        //拿到最大时间 最小时间段
        if (StrUtil.isNotBlank(query.getReturnTime())) {
            String[] split = query.getReturnTime().split("~");
            query.setMinCreateTime(split[0].trim());
            query.setMaxCreateTime(split[1].trim());
        }

        //开启分页查询
        Page<BookReturn> page = new Page<>(query.getPage(),query.getLimit());
        Page<BookReturn> bookReturnPage = bookReturnMapper.selectPage(page, new LambdaQueryWrapper<BookReturn>()
                .eq(!ObjectUtils.isEmpty(query.getRentId()), BookReturn::getBookId, query.getRentId())
                .between(!ObjectUtils.isEmpty(query.getReturnTime()), BookReturn::getReturnTime, query.getMinCreateTime(), query.getMaxCreateTime())
        );

        return new Result(bookReturnPage);
    }

    /**
     * 还书
     *
     * @param bookReturn
     * @return
     */
    @Override
    /*多张表查询 需要开启事务*/
    @Transactional(rollbackFor = Exception.class)
    public Result returnBook(BookReturn bookReturn) {
        // 1. 新增还书记录
        // 2. 修改图书状态
        //3. 修改借阅记录状态
        //图书id
        Integer bookId = bookReturn.getBookId();
        Book book = bookMapper.selectById(bookId);

        /**
         * 检查书籍借阅状态
         */
        if (book.getIsRent().equals(Constant.RENT_UN)){
            return new Result(CodeMsg.RENT_FLAG_ERROR );
        }
        //借阅记录ID
        Integer rentId = bookReturn.getRentId();
        BookRent bookRent = bookRentMapper.selectById(rentId);
        if (bookRent.getState().equals(Constant.RENTURN_ED )){
            return  new Result(CodeMsg.RENT_FLAG_ERROR);
        }
        //修改图书状态
        book.setIsRent(Constant.RENT_UN);
        bookMapper.updateById(book);

        //修改借阅记录状态
        bookRent.setState(Constant.RENTURN_ED);
        bookRentMapper.updateById(bookRent);

        //设置操作人
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        bookReturn.setRentName(activeUser.getRealName());

        //设置时间
        bookReturn.setCreateTime(new Date());

        bookReturnMapper.insert(bookReturn);
        return new Result();
    }
}
